package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.Banner;
import com.gxa.pingan.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJin
 * @date 2021/10/25 15:32
 * Description:
 **/
@CrossOrigin
@RestController
@RequestMapping(value = "/banner", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "执行轮播图操作的控制器", tags = "轮播图接口的api")
public class BannerController {

    @Resource
    private BannerService bannerService = null;

    /**
     * 添加轮播图的接口
     * @param banner 轮播图对象
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation("添加轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "banner", paramType = "body")
    })
    public Map<String, Object> insert(@RequestBody Banner banner ) {
        bannerService.insert(banner);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();

    }

    /**
     * 根据删除轮播图
     * @param id 轮播图编号
     * @return
     * @throws BusinessException
     */
    @PostMapping("/delete")
    @ApiOperation("删除轮播图")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true)
    public Map<String, Object> delete(@RequestParam("id") Long id) throws BusinessException {
        System.out.println("controller中的id：" + id);
        bannerService.delete(Long.valueOf(id));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    /**
     * 修改轮播图是否可见
     * @param id
     * @param type
     * @return
     * @throws BusinessException
     */
    @PostMapping("/updateType")
    @ApiOperation("修改轮播图是否可见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "type", paramType = "query", required = true)
    })
    public Map<String, Object> updateType(Long id, Boolean type) throws BusinessException {
        bannerService.updateType(id, type);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * 修改轮播图信息
     * @param banner 轮播图对象
     * @return
     * @throws BusinessException
     */
    @PostMapping("/update")
    @ApiOperation("修改轮播图信息")
    //@ApiImplicitParam(value = "banner", paramType = "body", required = true)
    public Map<String, Object> update(@RequestBody Banner banner) throws BusinessException {
        bannerService.update(banner);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * 上下移动轮播图
     * @param id 轮播图id
     * @param type 0：上移 1：下移
     * @return
     * @throws BusinessException
     */
    @PostMapping("/move")
    @ApiOperation("轮播图上下移动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "type", paramType = "query", required = true)
    })
    public Map<String, Object> move(Long id, Integer type) throws BusinessException {
        bannerService.move(id, type);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    /**
     * 分页获取轮播图
     * @param page 页码
     * @param size 每页长度
     * @return 返回对应页码的轮播图集合以及总轮播图数量
     * @throws BusinessException
     */
    @PostMapping("/getList")
    @ApiOperation("分页获取全部轮播图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = true)
    })
    public Map<String, Object> getList(Integer page, Integer size) throws BusinessException {
        List<Banner> listByPage = bannerService.getListByPage(page, size);
        Integer listSize = bannerService.getListSize();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        // 轮播图的总数量
        jsonResult.put("count",listSize);
        jsonResult.setData(listByPage);
        return jsonResult.getDataMap();
    }

}
